define(function (require) {

    require('../../../c/base/index');

    require('../../../c/nav/index');

    var tableData;
    var firstTime = true;
    var totalCount = 0;

    var Config = {
        init: function () {
            //初始化菜单选中效果
            $(".fa-circle-o").each(function () {
                if ($(this).parents("a").text() == "打印系统运单查询") {
                    $(this).parents("a").parents("li").addClass("active");
                }
            });
            this.initTable();
            this.initValidator();
            this.bindEvent();
        },
        /**
         * 执行查询操作.
         */
        doSearch: function () {
            if ($("#search_bizOrderId").val() || $("#serarch_value").val() || $("#search_logisticsNo").val()) {
                $("#query_form").data('bootstrapValidator').validate();
                if ($("#query_form").data('bootstrapValidator').isValid()) {
                    $('#shop_table').DataTable().ajax.reload();
                }
            } else {
                $("#msg_dialog_content").text("输入内容不能为空！");
                $('#msg_dialog').modal('show');
            }
        },
        /**
         * 初始化table
         */
        initTable: function () {
            var self = this;
            tableData = $("#shop_table").dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%", //表格的宽度
                "sScrollXInner": "100%", //表格的内容宽度
                "bScrollCollapse": true, //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": true, //是否显示分页
                "bLengthChange": true, //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": true, //显示表格信息
                "bAutoWidth": true, //自适应宽度
                "bServerSide": true, //开启服务端处理
                "sAjaxSource": CRM.getOuterUrl('/logisticscentral/crm/stockout/queryList'),
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    if (firstTime) {
                        firstTime = false;
                        return;
                    }
                    var _url = CRM.getOuterUrl('/logisticscentral/crm/stockout/queryList');
                    var queryData = {};
                    queryData['pageNum'] = aoData[3].value / aoData[4].value + 1;
                    queryData['pageSize'] = aoData[4].value;

                    if ($("#search_bizOrderId").val()) {
                        queryData['bizOrderId'] = $("#search_bizOrderId").val();
                    }
                    if ($("#serarch_value").val()) {
                        if ($("#search_type").val() == "mobile") {
                            queryData['buyerMobile'] = $("#serarch_value").val();
                        } else if ($("#search_type").val() == "orderMobile") {
                            queryData['submitUserMobile'] = $("#serarch_value").val();
                        }
                    }
                    if ($("#search_logisticsNo").val()) {
                        queryData['logisticsNo'] = $("#search_logisticsNo").val();
                    }
                    if (!($("#search_bizOrderId").val() || $("#serarch_value").val() || $("#search_logisticsNo").val())) {
                        return;
                    }
                    $.ajax({
                        "type": 'POST',
                        "url": _url,
                        "dataType": "json",
                        contentType: "application/json",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": JSON.stringify(queryData),
                        contentType: "application/json",
                        "success": function (req) {
                            if (req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    resultData.iTotalDisplayRecords = req.entry.length;
                                    resultData.iTotalRecords = totalCount;
                                    $.each(req.entry, function (key, entry) {
                                        var checkoutStatus = "";
                                        if (entry.checkoutStatus == 0) {
                                            checkoutStatus = "未出库";
                                        } else if (entry.checkoutStatus == 1) {
                                            checkoutStatus = "已出库";
                                        } else if (entry.checkoutStatus == -1) {
                                            checkoutStatus = "包裹拦截成功";
                                        }
                                        resultData.aaData.push([
                                            entry.bizOrderId,
                                            CRM.htmlEncode(entry.buyerNick),
                                            entry.buyerMobile,
                                            CRM.htmlEncode(entry.buyerCityName) + CRM.htmlEncode(entry.districtName) + CRM.htmlEncode(entry.buyerAddress),
                                            CRM.htmlEncode(entry.gmtCreate),
                                            CRM.htmlEncode(entry.requireSendTime),
                                            CRM.htmlEncode(entry.orderTypeName),
                                            CRM.htmlEncode(entry.logisticsCompanyName),
                                            entry.logisticsNo,
                                            CRM.htmlEncode(entry.senderCityName),
                                            entry.itemSequence,
                                            entry.isCancle ? "取消" : "正常",
                                            entry.printStatus ? "已经打印" : "未打印",
                                            checkoutStatus,
                                            CRM.htmlEncode(entry.itemName)
                                        ]);
                                    });
                                }

                            } else {
                                $("#msg_dialog_content").text(req.message);
                                $('#msg_dialog').modal('show');
                            }
                            fnCallback(resultData);
                        },
                        error: function (e) {
                            //请求出错处理
                            $("#msg_dialog_content").text("获取店铺列表,请求服务异常！");
                            $('#msg_dialog').modal('show');
                        }
                    });
                },
                "aaSorting": [
                    [1, "asc"]
                ], //给列表排序 ，第一个参数表示数组 (由0开始)。1 表示Browser列。第二个参数为 desc或是asc
                "aoColumns": [{
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": true
                }, {
                    "bSortable": false
                }, {
                    "bSortable": true
                }, {
                    "bSortable": false
                }

                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "full_numbers" //分页，一共两种样式，full_numbers和two_button(默认)

            });
        },
        /**
         * 表单验证初始化
         */
        initValidator: function () {
            $('#query_form').bootstrapValidator({
                message: '输入错误',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    search_bizOrderId: {
                        message: 'error',
                        validators: {
                            regexp: {
                                regexp: /^[0-9]+$/,
                                message: '请输入数字'
                            }
                        }
                    },
                    serarch_value: {
                        message: 'error',
                        validators: {
                            phone: {
                                country: 'CN',
                                message: '请输入正确的电话格式'
                            }
                        }
                    }
                }
            });
        },
        bindEvent: function () {
            var self = this;
            $("#search_btn").on('click', function () {
                self.doSearch();
            });
        }
    };

    window.doSearch = $.proxy(Config.doSearch, Config);

    Config.init();
});
